package yh.plugins.workflow;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beaut.project.dao.GDMapper;
import com.beaut.project.util.FlowUtility;
import com.beaut.project.util.SpringContextUtil;
import com.beaut.project.util.Utility;

import yh.core.data.YHRequestDbConn;
import yh.core.funcs.workflow.util.YHIWFPlugin;
import yh.core.global.YHBeanKeys;
import yh.core.util.PluginThread;

/**
 * 工作流插件接口 * 工单采购申请插件
 * @author jpt
 *
 */
public class GdPurchaseApplyPlugin extends BasePlugin implements YHIWFPlugin{
  /**
   * 节点执行前执行   * @param request
   * @param response
   * @return
   * @throws Exception 
   */
  public String before(HttpServletRequest request, HttpServletResponse response) throws Exception {
	  try {
	      YHRequestDbConn requestDbConn = (YHRequestDbConn) request
	          .getAttribute(YHBeanKeys.REQUEST_DB_CONN_MGR);
	      Connection conn = requestDbConn.getSysDbConn();
	      //流程id
	      String flowIdStr = request.getParameter("flowId");
	      //工作id即流水号
	      String runIdStr = request.getParameter("runId");
	      //用户选择的步骤号(不是实际步骤号),这时步骤号是以逗号分割的(并发时用户可能先多个步骤)，如：2,
	      String prcsChoose = request.getParameter("prcsChoose");
	      //设计步骤号,即现在执行所在的流程步骤
		  String flowPrcsStr = request.getParameter("flowPrcs");
	      int flowId = Integer.parseInt(flowIdStr);
	      String tableName=FlowUtility.getTableName(flowId);
	      if(tableName==null || tableName.equals("")){
	    	  return "代码中缺少相关配置，请联系开发商";
	      }
	      int updateStatus=this.updateBusinessStatus(tableName, FlowUtility.getTableFieldStatusName(flowId), prcsChoose, conn, runIdStr, flowId);
	      
	      String businessKey = this.getBusinessId(conn, runIdStr, flowId);
	      GDMapper gdMapper = (GDMapper) SpringContextUtil.getBean("gdMapper");
	      Map<String,Object> queryMap = new HashMap<String,Object>();
	      queryMap.put("apply_id", businessKey);
	      
	      if(updateStatus == noAgreed){
	    	  //X-->1
	    	  try{
	    		//更新审批占用数
	    		  gdMapper.purchaseApplyPluginBack(queryMap);
	    	  }
	    	  catch(Exception e){
	    		  gdMapper.purchaseApplyPluginForward(queryMap);
	    	  }
	      }
	      if(updateStatus == inFlow){
	    	  //1-->X
	    	  if("1".equals(flowPrcsStr)){
	    		  try{
	    			  //更新审批占用数
	    			  gdMapper.purchaseApplyPluginForward(queryMap);
	    		  }
	    		  catch(Exception e){
	    			  gdMapper.purchaseApplyPluginBack(queryMap);
	    		  }
	    	  }
	      }
	      //只有通过了才要回调
	      if(updateStatus == agreed){
//	    	  if(updateStatus==this.agreed || updateStatus==this.noAgreed){
	    	String sn=request.getServerName();
	        int port=request.getServerPort();
	        String cp=request.getContextPath();
	        String url=FlowUtility.getBackUrl(flowId);
	        if(Utility.isNullorEmpty(url)){
	        	return null;
	        }
	    	String urlBack="http://"+sn+":"+port+cp+url+"?key="+businessKey+"&status="+updateStatus;
	    	Cookie[] cookies = request.getCookies();
	    	String JSESSIONID = null;
	    	for(Cookie c:cookies){
	    		if("JSESSIONID".equals(c.getName())){  
                    JSESSIONID = c.getValue();  
                    break;  
                }  
	    	}
	    	Map<String, Object> sessionMap = (Map<String, Object>) request.getSession().getAttribute("LOGIN_USER");
	    	String personId= sessionMap.get("id").toString();
	    	String person=sessionMap.get("truename").toString();
	    	PluginThread pthread=new PluginThread(urlBack,JSESSIONID,personId,person);
	    	pthread.start();
	      }
	    } catch(Exception ex) {
	      throw ex;
	    }
    return null;
  }
  /**
   * 节点执行完毕执行
   * @param request
   * @param response
   * @return
   */
  public String after(HttpServletRequest request, HttpServletResponse response) throws Exception {
    return null;
  }
}
